<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/21  10:10
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;

Class DeliveryUserController extends BaseController {

    public function index() {
        $EMan = M("Delivery_man");
        $d = $EMan->select();
        $this->assign("data" , $d);
        $this->display();
    }

    function export(){
        $EMan = M("Delivery_man");
        $d = $EMan->select();

        $file = excel_export($d);
        ouput_Browser($file);
    }

    function del() {
        $man_id = I("get.man_id");
        $EMan = M("Delivery_man");
        $EMan->where("man_id={$man_id}")->delete();
        $count = $EMan->where("man_id={$man_id}")->count();
        if ($count == 0) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    function add() {
        $this->display("edit");
    }

    function edit() {
        $man_id = I("get.man_id");
        $EMan = M("Delivery_man");
        $d = $EMan->where("man_id={$man_id}")->find();
        $this->assign("data" , $d);
        $this->display();
    }

    function doEdit() {
        $man_id = I("get.man_id");
        $EMan = M('Delivery_man');
        if ($man_id == "") {
            //新增
            $data['name'] = I("post.name");
            $data['phone'] = I("post.phone");
            $data['account'] = I("post.account");
            $data['password'] = I("post.password");
            $de = $EMan->where(['account' => $data['account']])->find();
            if (!empty($de)) {
                $this->error('此账号已存在，请更换');
            }
            $EMan->startTrans();
            $man_id = $EMan->add($data);
            if ($man_id) {
                $EMan->commit();
                $this->success("新增成功",U("index"));
            } else {
                $EMan->rollback();
                $this->error("新增失败");
            }
        } else {
            //编辑
            $data['name'] = I("post.name");
            $data['phone'] = I("post.phone");
            $EMan->startTrans();
            $de=$EMan->where("man_id={$man_id}")->data($data)->save();
            if($de){
                $EMan->commit();
                $this->success("编辑成功",U("index"));
            }else{
                $EMan->rollback();
                $this->error("编辑失败");
            }
        }
    }
}